package edu.ntu.im.p2ptrade.entity;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class PeerItemRepository {
	private static PeerItemRepository me;
	
	private Map<String, List<Item>> peerItems = new HashMap<String, List<Item>>();
	private List<String> peerUpdateInfo = new LinkedList<String>(); 
	public static PeerItemRepository getInstance(){
		if (me == null){
			me = new PeerItemRepository();
		}
		return me;
	}
	private PeerItemRepository(){
	}
	
	public void setItems(String peer, List<Item> items){
		this.peerItems.put(peer, items);
		this.peerUpdateInfo.add(peer);
	}
	public boolean peerUpdated(String peer){
		return this.peerUpdateInfo.contains(peer);
	}
	
	public List<Item> fetchItems(String peer){
		this.peerUpdateInfo.remove(peer);
		return this.peerItems.get(peer);
	}
	public Item getItem(String peer, int i) {
		List<Item> items = this.peerItems.get(peer);
		Item item = null;
		for (Item cur:items){
			if (cur.getId() == i){
				item = cur;
				break;
			}
		}
		return item;
	}

}
